import { LOGIN_ROUTER } from "../constants";
import { GetUrl } from "./GetUrl"
import { GetUrlPanel } from "./GetUrlPanel"
import { CollectionManager } from "./CollectionManager"

export namespace ToLogin {
    export const id = 'TO_LOGIN'

    export enum Action {
        TO_LOGIN = 'TO_LOGIN'
    }

    export type Message = { id: string, action: Action }

    export const consume = (message: Message) => {
        // 跳转登录
        window.open(LOGIN_ROUTER, '_blank')
    }

    export const createToLoginAction = () => ({
        id,
        action: Action.TO_LOGIN
    })
}

const ReceiverMap = {
    [ToLogin.id]: ToLogin,
    [GetUrlPanel.id]: GetUrlPanel,
    [CollectionManager.id]: CollectionManager
}

/**
 * 消息接收处理
 */
export namespace MessageReceive {
    export type Message = GetUrlPanel.Message | ToLogin.Message | CollectionManager.Message

    export const consume = (message: Message) => {
        console.log('MessageReceive.consume called with message:', message);
        console.log('Message ID:', message.id);
        console.log('Available receivers:', Object.keys(ReceiverMap));
        console.log('Matching receiver:', ReceiverMap[message.id]);
        
        if (!ReceiverMap[message.id]) {
            console.error('No receiver found for message ID:', message.id);
            return { error: 'No receiver found for message ID: ' + message.id };
        }
        
        return ReceiverMap[message.id]?.consume(message);
    }
}